import java.util.*;


public class AnagramsHashMap {
	public void tester() {
		String [] a = {"","",""};
		anagrams(a);
	}
	public static List<String> anagrams(String[] strs) {  //this is all about index
	    List<String> results= new LinkedList<String>();

	    

	    Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();

	    for(int i = 0; i<strs.length; i++){
	        String key = sort(strs[i]);
	        List<Integer> list = map.get(key);
	        if(list == null){
	            list = new ArrayList<Integer>();
	            list.add(i);
	            map.put(key, list);
	        }else{
	            map.get(key).add(i);
	        }
	    }  // finish the hashmap

	    for(String key : map.keySet()){
	        if(map.get(key).size()>1){
	            for(int index : map.get(key)){
	                results.add(strs[index]);
	            }
	        }
	    }
	    
	    return results;
	}   
	
	public static String sort(String s){
	    StringBuffer sb = new StringBuffer();
	    int[] character = new int[26];
	    for(int i = 0; i<s.length(); i++){
	        int count = s.charAt(i)-'a';
	        character[count]++;
	    }

	    for(int i = 0; i<26; i++){
	        while(character[i]>0){
	            sb.append((char)('a'+i));
	            character[i]--;
	        }
	    }

	    return sb.toString();
	}
}
